<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Daftar_penduduk extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_daftar_penduduk');
	}
	
	function index(){
		$data['page_title']='Daftar Penduduk';
		$data['page_icon']='icon-group';
		$data['page_content']='daftar_penduduk';

		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		$data = $this->mdl_daftar_penduduk->getdata();
		echo $this->mdl_daftar_penduduk->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		
		$data['no_ktp'] = '';
		$data['nama'] = '';
		$data['agama'] = '';
		$data['t_lahir'] = '';
		$data['tgl_lahir'] = date('d/m/Y');
		$data['j_kel'] = '';
		$data['gol_darah'] = '';
		$data['w_negara'] = '';		
		$data['pendidikan'] = '';
		$data['pekerjaan'] = '';
		$data['s_nikah'] = '';
		$data['thn_daftar'] = '';
		$this->load->view('daftar_penduduk_form', $data);
	}
	
	function save(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("no_ktp", 'No KTP', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama", 'Nama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("agama", 'Agama', 'trim|required|xss_clean');
		$this->form_validation->set_rules("t_lahir", 'Tempat Lahir', 'trim|required|xss_clean');
		$this->form_validation->set_rules("tgl_lahir", 'Tanggal Lahir', 'trim|required|xss_clean');
		$this->form_validation->set_rules("j_kel", 'Jenis Kelamin', 'trim|required|xss_clean');
		$this->form_validation->set_rules("gol_darah", 'Golongan Darah', 'trim|required|xss_clean');
		$this->form_validation->set_rules("w_negara", 'Warga Negara', 'trim|required|xss_clean');
		$this->form_validation->set_rules("pendidikan", 'Pendidikan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("pekerjaan", 'Pekerjaan', 'trim|required|xss_clean');
		$this->form_validation->set_rules("s_nikah", 'Status', 'trim|required|xss_clean');
		$this->form_validation->set_rules("thn_daftar", 'Tahun Terdafttar', 'trim|required|xss_clean');		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			$result = $this->mdl_daftar_penduduk->InsertOnDb($data);
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	}

	function detail($no_ktp){
		$r = $this->mdl_daftar_penduduk->getdatadetail($no_ktp);
		
		$data['no_ktp'] = $r->row()->no_ktp;
		$data['nama'] = $r->row()->nama;
		$data['agama'] = $r->row()->agama;
		$data['t_lahir'] = $r->row()->t_lahir;
		$data['tgl_lahir'] = FormatDateFromMysql($r->row()->tgl_lahir); //$r->row()->tgl_lahir;
		#format J_KEL
		if($r->row()->j_kel == 'L'){
			$data['j_kel'] = 'Laki-laki';
		}else{
			$data['j_kel'] = 'Perempuan';
		}
		$data['gol_darah'] = $r->row()->gol_darah;
		$data['w_negara'] = $r->row()->w_negara;
		$data['pendidikan'] = $r->row()->pendidikan;
		$data['pekerjaan'] = $r->row()->pekerjaan;
		$data['s_nikah'] = $r->row()->s_nikah;

		$this->load->view('daftar_penduduk_detail', $data);
	}

	#export excel
	function exportExcel(){
		//load our new PHPExcel library
		$this->load->library('excel');
		
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('Laporan Data Penduduk');

		# Init Header
		//$this->excel->getActiveSheet()->getColumnDimension('A:AB')->setAutoSize(true);
		//$this->excel->getActiveSheet()->getStyle('A1:AB2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Data Penduduk')
									  ->setCellValue('A3', 'No KTP')
									  ->setCellValue('B3', 'Nama')
									  ->setCellValue('C3', 'Agama')
									  ->setCellValue('D3', 'Tempat Lahir')
									  ->setCellValue('E3', 'Tanggal Lahir')
									  ->setCellValue('F3', 'JK')
									  ->setCellValue('G3', 'Gol Darah')
									  ->setCellValue('H3', 'Warga Negara')
									  ->setCellValue('I3', 'Pendidikan')
									  ->setCellValue('J3', 'Pekerjaan')
									  ->setCellValue('K3', 'Status');

		#iterasi
		$curpos = 4;
		$kk = $this->mdl_daftar_penduduk->getDataExcel();
		foreach ($kk->result() as $row) {
			# code...
			$this->excel->getActiveSheet()->setCellValueExplicitByColumnAndRow(0, $curpos, $row->no_ktp, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(1, $curpos, $row->nama, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(2, $curpos, $row->agama, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(3, $curpos, $row->t_lahir, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(4, $curpos, $row->tgl_lahir, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(5, $curpos, $row->j_kel, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(6, $curpos, $row->gol_darah, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(7, $curpos, $row->w_negara, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(8, $curpos, $row->pendidikan, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(9, $curpos, $row->pekerjaan, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(10, $curpos, $row->s_nikah, PHPExcel_Cell_DataType::TYPE_STRING);
			
			$curpos++;						  	  
		}
		#style
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('A3:K3')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->excel->getActiveSheet()->mergeCells('A1:K1');
		$this->excel->getActiveSheet()->getStyle('A1:K1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		# init Column Color
		$this->excel->getActiveSheet()->getStyle('A3:K3')->getFill()
					->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
					->getStartColor()->setRGB('34efbd');

		# set border
		$styleArray = array(
			'borders' => array(
				'outline' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('rgb' => '000000'),
				),
				'inside' => array(
					'style' => PHPExcel_Style_Border::BORDER_HAIR	,
					'color' => array('rgb' => '000000'),
				),
			),
		);
		$this->excel->getActiveSheet()->getStyle('A3:K'.($curpos-1))->applyFromArray($styleArray);

		# begin download
		$filename= 'Laporan Penduduk.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
					 
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');

	}

}